package Controller1;

import javafx.fxml.FXML;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import Dao.goodsDao;
import Dao.staffDao;
import Model1.goods;
import Model1.staff;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseDragEvent;
import javafx.stage.Stage;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.control.TableView;

import javafx.scene.control.TableColumn;

public class StaffController {
	@FXML
	private TableView<staff> tb_staff = new TableView<>();
	@FXML
	private TableColumn tb_name;
	@FXML
	private TableColumn tb_id;
	@FXML
	private TableColumn tb_password;
	@FXML
	private TableColumn tb_level;
	@FXML
	private TableColumn tb_phone;
	@FXML
	private Button change;
	@FXML
	private Button delete;
	@FXML
	private Button search;
	@FXML
	private Button back;
	@FXML
	private TextField id;
	@FXML
	private TextField name;
	@FXML
	private TextField password;
	@FXML
	private TextField level;
	@FXML
	private TextField phone;
	
	public void initialize() {
	
		b();
		tb_staff.addEventFilter(MouseDragEvent.MOUSE_CLICKED, event -> {
			if (tb_staff.getSelectionModel().getSelectedItem()!= null) {
				staff stf = tb_staff.getSelectionModel().getSelectedItem();
				
				id.setText(stf.getId()+"");
				name.setText(stf.getName());
				level.setText(stf.getLevel()+"");
				phone.setText(stf.getPhonenumber()+"");
				password.setText(stf.getPassword()+"");
				
			}
		});

	}
	
	public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("View1/Table_staff.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.setResizable(false);
        primaryStage.show();
    }
	
	public void refresh(List<staff> staff_Table) {
		ObservableList<staff> data = FXCollections.observableArrayList(staff_Table);
		tb_id.setCellValueFactory(new PropertyValueFactory<>("id"));
		tb_name.setCellValueFactory(new PropertyValueFactory<>("name"));
		tb_level.setCellValueFactory(new PropertyValueFactory<>("level"));
		tb_phone.setCellValueFactory(new PropertyValueFactory<>("phonenumber"));
		tb_password.setCellValueFactory(new PropertyValueFactory<>("password"));
		
		
		tb_staff.setItems(data);
		tb_staff.getSelectionModel().clearSelection();
		//image.setImage(null);
	}
	
	public void delete() {
		try {
			int sid=Integer.parseInt(id.getText());
			Boolean a=staffDao.Delete(sid);	
		}catch(Exception e) {
			JOptionPane.showMessageDialog(null, "删除数据错误，请重试"); 
			e.printStackTrace();
		}
		b();
	}
	
	public void search() {
		try {
			int sid=Integer.parseInt(id.getText());
			staff stf=staffDao.SearchById(sid);
			List<staff> staff_group=new ArrayList<>();
			staff_group.add(stf);
			refresh(staff_group);
		}catch(Exception e) {
			JOptionPane.showMessageDialog(null, "查找数据错误，请重试"); 
			e.printStackTrace();
		}
	}
	
	public void change() {
		try {
			
			staff stf=new staff(name.getText(), Integer.parseInt(id.getText()),level.getText(),phone.getText(),password.getText());
			Boolean a=staffDao.Update(stf);
		}catch(Exception e) {
			JOptionPane.showMessageDialog(null, "修改数据错误，请重试"); 
			e.printStackTrace();
		}
		b();
	}
	
	public void b() {
		refresh(staffDao.findAll());
	}
	
	public void back() {
		Stage stage = (Stage)name.getScene().getWindow();
		stage.close();
		MainViewController mc = new MainViewController();
		try {
			mc.start(stage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
